<?php
namespace app\api\forms\elements;

use Phalcon\Tag;
use Phalcon\Forms\Element;

class JeDateElement extends Element
{

    public function render($attributes = null)
    {
        $form = $this->getForm();
        if(!isset($form->hasJedateJs)||!$form->hasJedateJs){
            $form->assets->addJs('jedate/jedate.js');
            $form->hasJedateJs = true;
        }
        $id = $this->getName();

        $options = json_encode($this->getClientOptions($id));
        $form->assets->addInlineJs("jeDate($options);",null,[$id]);

        $html = Tag::textField($this->prepareAttributes($attributes));
        return $html;
    }

    /**
     * Returns the options for the JS widget.
     * @return array the options
     */
    protected function getClientOptions($id)
    {
        $options = [
            'dateCell'=> "#$id", //isinitVal:true,
            'format'=> 'YYYY-MM-DD',
            'isTime'=> false,
            //'isClear'=>true,
        ];

        return $options;
    }
}